package com.inspur.cloud.controller.auth;

import com.inspur.cloud.entity.dataspace.ao.auth.ResourceAuthInfoAO;
import com.inspur.cloud.entity.dataspace.ao.user.UserAO;
import com.inspur.cloud.entity.dataspace.common.OpEntityResult;
import com.inspur.cloud.entity.dataspace.common.OpResult;
import com.inspur.cloud.entity.dataspace.vo.auth.AuthUpdateVO;
import com.inspur.cloud.exception.ApiException;
import com.inspur.cloud.service.dataspace.AuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;
import java.util.List;

@RestController
@RequestMapping("/dataspace/api/v1/auth")
@Api(value = "auth", description = "权限控制")
public class AuthController {

    @Autowired
    private AuthService authService;

    @GetMapping("/getInfo")
    @ApiOperation(value = "权限信息", notes = "对接人：soff")
    @RolesAllowed({"Admin", "Tenant"})
    public OpResult getInfo(@ApiParam(value = "资源id") Integer id, @ApiParam(value = "资源类型 例如：HDFS HIVE YARN HBASE KAFKA") String type) {
        if (id == null || id == 0) {
            throw new ApiException("资源id不能为空");
        }
        if (StringUtils.isEmpty(type)) {
            throw new ApiException("资源类型不能为空");
        }
        if (!type.equalsIgnoreCase("HDFS") &&
                !type.equalsIgnoreCase("HIVE") &&
                !type.equalsIgnoreCase("YARN") &&
                !type.equalsIgnoreCase("HBASE") &&
                !type.equalsIgnoreCase("KAFKA")) {
            throw new ApiException("此资源类型不支持");
        }
        OpEntityResult<ResourceAuthInfoAO> result = new OpEntityResult();
        result.setCode(1);
        result.setMsg("查询成功");
        result.setData(authService.getInfo(id, type));
        return result;
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改权限", notes = "对接人：soff")
    @RolesAllowed({"Admin", "Tenant"})
    public OpResult updateInfo(@RequestBody AuthUpdateVO authUpdate) {
        Integer id = authUpdate.getId();
        String type = authUpdate.getType();
        if (id == null || id == 0) {
            throw new ApiException("资源id不能为空");
        }
        if (StringUtils.isEmpty(type)) {
            throw new ApiException("资源类型不能为空");
        }
        if (!type.equalsIgnoreCase("HDFS") &&
                !type.equalsIgnoreCase("HIVE") &&
                !type.equalsIgnoreCase("YARN") &&
                !type.equalsIgnoreCase("HBASE") &&
                !type.equalsIgnoreCase("KAFKA")) {
            throw new ApiException("此资源类型不支持");
        }
        OpResult result = new OpResult();
        authService.updateInfo(authUpdate);
        result.setCode(1);
        result.setMsg("修改成功");
        return result;
    }

    @GetMapping("/userList")
    @ApiOperation(value = "可被修改权限的人员列表", notes = "对接人：soff")
    @RolesAllowed({"Admin", "Tenant"})
    public OpResult userList(@ApiParam(value = "资源id") Integer resourceId, @ApiParam(value = "资源类型 例如：HDFS HIVE YARN HBASE KAFKA") String type,
                             @ApiParam(value = "查询情形  isTenantGetTen为1 表示租户获取租户列表，非1表示获取下级列表") String isTenantGetTen,
                             @ApiParam(value = "模糊查询用户名") String username) {
        if (resourceId == null || resourceId == 0) {
            throw new ApiException("资源id不能为空");
        }
        if (StringUtils.isEmpty(type)) {
            throw new ApiException("资源类型不能为空");
        }
        if (!type.equalsIgnoreCase("HDFS") &&
                !type.equalsIgnoreCase("HIVE") &&
                !type.equalsIgnoreCase("YARN") &&
                !type.equalsIgnoreCase("HBASE") &&
                !type.equalsIgnoreCase("KAFKA")) {
            throw new ApiException("此资源类型不支持");
        }
        OpEntityResult<List<UserAO>> result = new OpEntityResult();
        result.setData(authService.userList(resourceId, type, username, "1".equals(isTenantGetTen)));
        result.setCode(1);
        result.setMsg("查询成功");
        return result;
    }
}
